package com.edu.hre.struts.action.survey;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.entity.HrePageInfo;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.db.pojo.HreSubunit;
import com.edu.hre.db.pojo.HreSurveyTopic;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.survey.SurveyManagementListForm;
import com.edu.hre.struts.form.survey.SurveySummaryDetailForm;
import com.edu.hre.struts.form.survey.SurveySummaryListForm;

public class SurveySummaryListAction extends DispatchAction {
	
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);
		HreUser user = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
		SurveySummaryListForm beanForm = (SurveySummaryListForm) form;
		beanForm.setCanAdd(userRoleScreen.isCanCreate());
		beanForm.setCanDelete(userRoleScreen.isCanDelete()); 
		
		String sortBy = RequestUtils.getParameterStr(request, "sort", null);
		if (sortBy != null) { 
			beanForm.setAsc(!beanForm.isAsc());
			CommonUtils.sortCollection(beanForm.getListObjs(), beanForm.isAsc(), sortBy);
			return mapping.findForward("surveySummaryList");
		} 
		List<HreUser> listUser = baseService.getPojos(null, -1, null, null, null, 0, 0, HreUser.class);
		List<HreSubunit> listSubunit = baseService.getPojos(null, -1, null, null, null, 0, 0, HreSubunit.class);
		
		int currentPage = RequestUtils.getParameterInt(request, "page", 1);
		int pageSize = CommonUtils.parseInt(DynamicConfig.getConfig(DynamicConfig.PAGE_SIZE), 20);
		List<SurveySummaryDetailForm> listObjs = new ArrayList<SurveySummaryDetailForm>();
		List<HreSurveyTopic> listTopics = baseService.getPojosOr(
				new String[]{"userCreated", "showToAll"},
				new int[]{Compare.EQUAL, Compare.EQUAL}, 
				new Object[]{user.getId(), true}, 
				new String[]{"endDate"}, new Boolean[]{true},  pageSize, pageSize * (currentPage - 1), HreSurveyTopic.class);
		for(int i = 0; listTopics != null && i < listTopics.size(); i ++){
			HreSurveyTopic topic = listTopics.get(i);
			SurveySummaryDetailForm obj = new SurveySummaryDetailForm(topic.getId(), topic.getName(),  
					SurveyManagementListAction.getSubunit(listSubunit, topic.getSubunitId()), topic.getStartDate(),
					topic.getEndDate(), SurveyManagementListAction.getUser(listUser, topic.getUserCreated()),
					topic.getEndDate().compareTo(new Date()) <= 0);
			listObjs.add(obj);
		}
		beanForm.setListObjs(listObjs);
		
		int totalItems = baseService.countByColumnsOr(
				new String[]{"userCreated", "showToAll"},
				new int[]{Compare.EQUAL, Compare.EQUAL}, 
				new Object[]{user.getId(), true}, "id", HreSurveyTopic.class);
		List<HrePageInfo> listPages = CommonUtils.getDisplayingPages(currentPage, CommonUtils.getTotalPage(totalItems));
		beanForm.setListPages(listPages);
		beanForm.setTotalItems(totalItems);
		beanForm.setTotalPage(CommonUtils.getTotalPage(totalItems));
		
		return mapping.findForward("surveySummaryList");
	}
}
